﻿// Decompiled with JetBrains decompiler
// Type: DnProcessSimulateCommands.AutomaticInterferenceVolumeCreation.CUIAutomaticInterferenceVolumeCreationDlg
// Assembly: DnProcessSimulateCommands, Version=2402.0.0.890, Culture=neutral, PublicKeyToken=null
// MVID: A885D881-AEDF-447E-852A-6F13102E089C
// Assembly location: C:\Program Files\Tecnomatix_2402\eMPower\DotNetCommands\DnProcessSimulateCommands.dll

using System;
using System.CodeDom.Compiler;
using System.Collections;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Diagnostics;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Forms;
using System.Windows.Markup;
using System.Windows.Media;
using System.Windows.Navigation;
using Tecnomatix.Engineering;
using Tecnomatix.Engineering.PrivateImplementationDetails;
using Tecnomatix.Engineering.Ui;
using Tecnomatix.Engineering.Ui.WPF;


namespace DnProcessSimulateCommands.AutomaticInterferenceVolumeCreation;

public class CUIAutomaticInterferenceVolumeCreationDlg : 
  TxWindow,
  INotifyPropertyChanged,
  IComponentConnector
{
  private const int DIALOG_MIN_WIDTH = 355;
  private const int DIALOG_MIN_HEIGHT = 420;
  private const int SIMULATION_PROGRESS_BAR_INITIAL_STEP = 4;
  private const int ADD_OPERATION = 1;
  private const int ADD_PROGRAM = 2;
  private int midLevelCurrentStep;
  private int midLevelAllSteps;
  private bool shouldStop;
  private bool invalidItems;
  private System.Windows.Size settingsSize;
  private CApAIVCManager mgr;
  private double lowLevelSimulationOverallDuration;
  private double lowLevelSimulationCurrentStep;
  private TxResourceDictionary baseRD;
  private AddButton addOperationsAndProgramsBtn;
  private AddButton addOperationsBtn;
  private AddButton addProgramsBtn;
  private TxSweptVolume_MayProceedCalculation mayProceedSVCallBack;
  private TxInterferenceVolume_MayProceedCalculation mayProceedIVCallBack;
  private bool showConfirmStopMsg = true;
  private CUIAutomaticInterferenceVolumeCreationDlg.DialogState dlgState;
  private CUIAutomaticInterferenceVolumeCreationDlg.ProgressState progressState;
  private bool wasCreationStarted;
  internal CUIAutomaticInterferenceVolumeCreationDlg rootWindow;
  internal System.Windows.Controls.Button CreateBtn;
  internal System.Windows.Controls.Button StopCloseBtn;
  internal Expander ProgressExp;
  internal PictureBox init_Image;
  internal PictureBox gen_SV_Image;
  internal PictureBox detecting_coll_Image;
  internal PictureBox gen_IV_Image;
  internal PictureBox det_Synchr_Locs_Image;
  internal PictureBox del_SV_Image;
  internal TextBlock deleting_SV_objects_TextBlock;
  internal TextBlock lowLevelPreMsg;
  internal TextBlock lowLevelMsg;
  internal System.Windows.Controls.ProgressBar lowLevelPrsBar;
  internal StackPanel errorPanel;
  internal System.Windows.Controls.Image _imError;
  internal TextBlock errors_reported;
  internal Hyperlink logFileHyperlink;
  internal Expander SettingsExp;
  internal Grid settingsGrid;
  internal StackPanel ButtonsToolbar;
  internal System.Windows.Controls.Button RemoveBtn;
  internal System.Windows.Controls.DataGrid operationsProgramsGrid;
  internal TextBlock NumAccuracy;
  internal Slider AccuracySlr;
  internal System.Windows.Controls.CheckBox keepNISV;
  internal System.Windows.Controls.CheckBox includeCables;
  private bool _contentLoaded;

  private event NextHighLevelStepStartedEventHandler NextHighLevelStepHandler;

  private event NextMidLevelStepStartedEventHandler NextMidLevelStepHandler;

  private event LowLevelStepDurationEventHandler LowLevelDurationHandler;

  private event LowLevelStepProgressEventHandler LowLevelProgresHandler;

  private event CApAIVCLogErrorEventHandler LogErrorEventHandler;

  public CUIAutomaticInterferenceVolumeCreationDlg()
  {
    this.InitializeComponent();
    if (TxApplication.ActiveDocument.SimulationPlayer.IsSimulationRunning())
      this.CreateBtn.IsEnabled = false;
    else
      this.CreateBtn.IsEnabled = this.ShouldEnableCreateButton();
    this.shouldStop = false;
    this.invalidItems = false;
    // ISSUE: method pointer
    this.mayProceedIVCallBack = new TxInterferenceVolume_MayProceedCalculation((object) this, __methodptr(MayProceed));
    // ISSUE: method pointer
    this.mayProceedSVCallBack = new TxSweptVolume_MayProceedCalculation((object) this, __methodptr(MayProceed));
    this.LowLevelDurationHandler += new LowLevelStepDurationEventHandler(this.CUIAutomaticInterferenceVolumeCreationDlg_LowLevelDurationHandler);
    this.LowLevelProgresHandler += new LowLevelStepProgressEventHandler(this.CUIAutomaticInterferenceVolumeCreationDlg_LowLevelProgresHandler);
    this.NextHighLevelStepHandler += new NextHighLevelStepStartedEventHandler(this.CUIAutomaticInterferenceVolumeCreationDlg_NextHighLevelStep);
    this.NextMidLevelStepHandler += new NextMidLevelStepStartedEventHandler(this.CUIAutomaticInterferenceVolumeCreationDlg_NextMidLevelStepHandler);
    this.LogErrorEventHandler += new CApAIVCLogErrorEventHandler(this.CUIAutomaticInterferenceVolumeCreationDlg_LogErrorHandler);
    this.mgr = new CApAIVCManager(this.mayProceedIVCallBack, this.mayProceedSVCallBack);
    this.baseRD = new TxResourceDictionary();
    this.baseRD.EnableFlatStyle = true;
    foreach (UIElement element in this.CreateAddButtonsAndFixColumnName())
      this.ButtonsToolbar.Children.Insert(0, element);
    TxApplication.ActiveDocument.Selection.Cleared += new TxSelection_ClearedEventHandler(this.Selection_Cleared);
    TxApplication.ActiveDocument.Selection.ItemsAdded += new TxSelection_ItemsAddedEventHandler(this.Selection_ItemsAdded);
    TxApplication.ActiveDocument.Selection.ItemsRemoved += new TxSelection_ItemsRemovedEventHandler(this.Selection_ItemsRemoved);
    TxApplication.ActiveDocument.Selection.ItemsSet += new TxSelection_ItemsSetEventHandler(this.Selection_ItemsSet);
    TxApplication.ActiveDocument.SimulationPlayer.SimulationStartedForward += new TxSimulationPlayer_SimulationStartedForwardEventHandler(this.SimulationPlayer_SimulationStartedForward);
    TxApplication.ActiveDocument.SimulationPlayer.SimulationStartedBackward += new TxSimulationPlayer_SimulationStartedBackwardEventHandler(this.SimulationPlayer_SimulationStartedBackward);
    TxApplication.ActiveDocument.SimulationPlayer.SimulationStopped += new TxSimulationPlayer_SimulationStoppedEventHandler(this.SimulationPlayer_SimulationStopped);
    TxApplication.ActiveDocument.SimulationPlayer.SimulationEndedForward += new TxSimulationPlayer_SimulationEndedForwardEventHandler(this.SimulationPlayer_SimulationEndedForward);
    TxApplication.ActiveDocument.SimulationPlayer.SimulationEndedBackward += new TxSimulationPlayer_SimulationEndedBackwardEventHandler(this.SimulationPlayer_SimulationEndedBackward);
    this.mgr.NextMidLevelStep += this.NextMidLevelStepHandler;
    this.mgr.NextHighLevelStep += this.NextHighLevelStepHandler;
    this.mgr.LowLevelStepProgress += this.LowLevelProgresHandler;
    this.mgr.LowLevelStepDuration += this.LowLevelDurationHandler;
    this.mgr.LogError += this.LogErrorEventHandler;
    ((FrameworkElement) this).MinHeight = 420.0;
    ((FrameworkElement) this).MinWidth = 355.0;
    this.ShouldAutoPosition = true;
    this.ShouldCloseOnDocumentUnloading = true;
    this.settingsSize = new System.Windows.Size();
    this.dlgState = CUIAutomaticInterferenceVolumeCreationDlg.DialogState.Settings;
    this.progressState = CUIAutomaticInterferenceVolumeCreationDlg.ProgressState.None;
    this.ShouldEnableAddBtn(TxApplication.ActiveDocument.Selection.GetAllItems());
    this._imError.Source = TxImageManager.GetImageByKey("ErrorColor").ConvertTo<ImageSource>(new System.Drawing.Size());
  }

  private bool MayProceed(int progress)
  {
    if (progress >= 0 && progress <= 100 && (double) progress > this.lowLevelPrsBar.Value)
      this.lowLevelPrsBar.Value = (double) progress;
    System.Windows.Forms.Application.DoEvents();
    return !this.shouldStop;
  }

  private void CUIAutomaticInterferenceVolumeCreationDlg_LowLevelProgresHandler(
    object sender,
    EventArgs args)
  {
    if (this.dlgState == CUIAutomaticInterferenceVolumeCreationDlg.DialogState.Settings)
      return;
    ++this.lowLevelSimulationCurrentStep;
    this.lowLevelPrsBar.Value = this.lowLevelSimulationCurrentStep * 100.0 / this.lowLevelSimulationOverallDuration;
  }

  private void CUIAutomaticInterferenceVolumeCreationDlg_LowLevelDurationHandler(
    object sender,
    LowLevelStepDurationEventArgs args)
  {
    if (this.dlgState == CUIAutomaticInterferenceVolumeCreationDlg.DialogState.Settings)
      return;
    this.lowLevelSimulationOverallDuration = args.Duration;
    if (this.lowLevelSimulationOverallDuration == 0.0)
      this.lowLevelSimulationOverallDuration = 0.1;
    this.lowLevelSimulationCurrentStep = 0.0;
  }

  private void CUIAutomaticInterferenceVolumeCreationDlg_NextMidLevelStepHandler(
    object sender,
    NextMidLevelStepStartedEventArgs args)
  {
    if (this.dlgState == CUIAutomaticInterferenceVolumeCreationDlg.DialogState.Settings)
      return;
    this.midLevelAllSteps = args.Steps;
    ++this.midLevelCurrentStep;
    int num1 = 0;
    double num2 = (double) ((this.midLevelCurrentStep - 1) * 100 / this.midLevelAllSteps);
    string str = "";
    switch (this.progressState)
    {
      case CUIAutomaticInterferenceVolumeCreationDlg.ProgressState.GeneratingSweptVolumeObjects:
        str = string.Format(this.mgr.ResourceMgr.GetString("AIVC_SV_MID_LEVEL_MSG"), (object) this.midLevelCurrentStep.ToString(), (object) this.midLevelAllSteps.ToString(), (object) ((int) num2).ToString());
        num1 = 4;
        break;
      case CUIAutomaticInterferenceVolumeCreationDlg.ProgressState.DetectingCollisions:
        str = string.Format(this.mgr.ResourceMgr.GetString("AIVC_COLL_SETS_MID_LEVEL_MSG"), (object) this.midLevelCurrentStep.ToString(), (object) this.midLevelAllSteps.ToString(), (object) ((int) num2).ToString());
        break;
      case CUIAutomaticInterferenceVolumeCreationDlg.ProgressState.GeneratingInterferenceVolumeObjects:
        str = string.Format(this.mgr.ResourceMgr.GetString("AIVC_IV_MID_LEVEL_MSG"), (object) this.midLevelCurrentStep.ToString(), (object) this.midLevelAllSteps.ToString(), (object) ((int) num2).ToString());
        num1 = 4;
        break;
      case CUIAutomaticInterferenceVolumeCreationDlg.ProgressState.DetectingSynchronizingLocations:
        str = string.Format(this.mgr.ResourceMgr.GetString("AIVC_SYNCHR_LOC_MID_LEVEL_MSG"), (object) this.midLevelCurrentStep.ToString(), (object) this.midLevelAllSteps.ToString(), (object) ((int) num2).ToString());
        num1 = 4;
        break;
      case CUIAutomaticInterferenceVolumeCreationDlg.ProgressState.DeletingNonIntersectingSweptVolumeObjects:
        str = string.Format(this.mgr.ResourceMgr.GetString("AIVC_NOW_DELETING"), (object) this.midLevelCurrentStep.ToString(), (object) this.midLevelAllSteps.ToString(), (object) ((int) num2).ToString());
        break;
    }
    this.lowLevelMsg.Text = str;
    this.lowLevelPrsBar.Value = this.lowLevelPrsBar.Minimum + (double) num1;
    System.Windows.Forms.Application.DoEvents();
  }

  private void CUIAutomaticInterferenceVolumeCreationDlg_NextHighLevelStep(
    object sender,
    EventArgs args)
  {
    if (this.dlgState == CUIAutomaticInterferenceVolumeCreationDlg.DialogState.Settings)
      return;
    this.AdvanceHighLevelStep();
  }

  private void CUIAutomaticInterferenceVolumeCreationDlg_LogErrorHandler(
    object sender,
    CApAIVCLogErrorEventArgs args)
  {
    if (this.dlgState == CUIAutomaticInterferenceVolumeCreationDlg.DialogState.Settings)
      return;
    this.errorPanel.Visibility = Visibility.Visible;
    this.logFileHyperlink.NavigateUri = new Uri(CApAIVCLogManager.FilePath);
    this.deleting_SV_objects_TextBlock.Foreground = (System.Windows.Media.Brush) System.Windows.Media.Brushes.Gray;
    this.errors_reported.Text = string.Format(this.mgr.ResourceMgr.GetString("AIVC_ERROR_REPROTED"), (object) args.NumErrors.ToString());
    System.Windows.Forms.Application.DoEvents();
  }

  private void RefreshSize()
  {
    if (this.ProgressExp.IsExpanded)
    {
      // ISSUE: explicit non-virtual call
      this.ProgressExp.Height = __nonvirtual (((FrameworkElement) this).Height) - 114.0;
    }
    else
      this.ProgressExp.Height = 23.0;
  }

  private void SaveDialogSize()
  {
    // ISSUE: explicit non-virtual call
    this.settingsSize.Height = __nonvirtual (((FrameworkElement) this).Height);
    // ISSUE: explicit non-virtual call
    this.settingsSize.Width = __nonvirtual (((FrameworkElement) this).Width);
    ((FrameworkElement) this).MaxHeight = 420.0;
    ((FrameworkElement) this).MaxWidth = 355.0;
  }

  private void LoadDialogSize()
  {
    ((FrameworkElement) this).MaxHeight = (double) int.MaxValue;
    ((FrameworkElement) this).MaxWidth = (double) int.MaxValue;
    // ISSUE: explicit non-virtual call
    __nonvirtual (((FrameworkElement) this).Height) = this.settingsSize.Height;
    // ISSUE: explicit non-virtual call
    __nonvirtual (((FrameworkElement) this).Width) = this.settingsSize.Width;
  }

  private void LoadMinDialogSize()
  {
    // ISSUE: explicit non-virtual call
    __nonvirtual (((FrameworkElement) this).Height) = 420.0;
    // ISSUE: explicit non-virtual call
    __nonvirtual (((FrameworkElement) this).Width) = 355.0;
  }

  private bool AreInvalidItemsInGrid
  {
    set
    {
      this.invalidItems = value;
      this.FirePropertyChanged("IsHeaderImageVisible");
    }
    get => this.invalidItems;
  }

  public bool IsHeaderImageVisible => this.invalidItems;

  private string SliderValue
  {
    get
    {
      string linearUnitName = TxApplication.Options.Units.LinearUnitName;
      return $"{Math.Round(this.AccuracySlr.Value / TxApplication.Options.Units.LinearMultiplier, Math.Min(TxApplication.Options.Units.LinearDigitsAfterDecimalPoint, 2)).ToString()} {linearUnitName}";
    }
  }

  private void EnableDisableControls(bool enable)
  {
    this.ButtonsToolbar.IsEnabled = enable;
    this.StopCloseBtn.IsEnabled = enable;
    this.AccuracySlr.IsEnabled = enable;
    this.keepNISV.IsEnabled = enable;
    this.ProgressExp.IsEnabled = !enable;
  }

  private void CreateBtn_Click(object sender, RoutedEventArgs e)
  {
    if (this.dlgState == CUIAutomaticInterferenceVolumeCreationDlg.DialogState.Settings)
    {
      this.EnableDisableControls(false);
      bool? isChecked = this.keepNISV.IsChecked;
      bool flag = true;
      this.deleting_SV_objects_TextBlock.Foreground = !(isChecked.GetValueOrDefault() == flag & isChecked.HasValue) ? (System.Windows.Media.Brush) System.Windows.Media.Brushes.Black : (System.Windows.Media.Brush) System.Windows.Media.Brushes.Gray;
      this.CreateBtn.Content = (object) this.mgr.ResourceMgr.GetString("AIVC_STOP");
      this.ProgressExp.IsExpanded = true;
      this.dlgState = CUIAutomaticInterferenceVolumeCreationDlg.DialogState.Progress;
      this.AdvanceHighLevelStep();
      this.shouldStop = false;
      this.wasCreationStarted = true;
      this.mgr.Generate(this.operationsProgramsGrid.Items, this.AccuracySlr.Value, this.keepNISV.IsChecked, this.includeCables.IsChecked);
      if (this.shouldStop)
        return;
      this.AdvanceHighLevelStep();
      this.ShowShouldOpenLogFile(this.mgr.ResourceMgr.GetString("AIVC_CREATION_PROCESS_FINISHED"));
      ((Window) this).Close();
    }
    else
    {
      if (TxDoNotShowAgainMessageBox.Show(this.mgr.ResourceMgr.GetString("AIVC_CONFIRM_STOP_GENERATION_MSG"), this.mgr.ResourceMgr.GetString("AIVC_NAME"), "AUTOMATIC_INTERFERENCE", MessageBoxButtons.YesNo, MessageBoxIcon.Question, DialogResult.Yes) != DialogResult.Yes)
        return;
      this.ShowShouldOpenLogFile(this.mgr.ResourceMgr.GetString("AIVC_CREATION_PROCESS_STOPPED"));
      this.shouldStop = true;
      this.mgr.StopGeneration(true);
      this.EnableDisableControls(true);
      this.deleting_SV_objects_TextBlock.Foreground = (System.Windows.Media.Brush) System.Windows.Media.Brushes.Gray;
      this.SettingsExp.IsExpanded = true;
      this.dlgState = CUIAutomaticInterferenceVolumeCreationDlg.DialogState.Settings;
      this.CreateBtn.Content = (object) this.mgr.ResourceMgr.GetString("AIVC_CREATE");
      this.ResetProgressExpander();
    }
  }

  private void ShowShouldOpenLogFile(string descr)
  {
    CUIAIVQResult cuiaivqResult = new CUIAIVQResult(CApAIVCLogManager.FilePath, descr, (Window) this);
    bool? nullable = ((Window) cuiaivqResult).ShowDialog();
    if (((int) nullable ?? 1) == 0)
      return;
    nullable = cuiaivqResult.ViewLogFile;
    if (((int) nullable ?? 1) == 0)
      return;
    CApAIVCLogManager.OpenFile();
  }

  private void ResetProgressExpander()
  {
    this.progressState = CUIAutomaticInterferenceVolumeCreationDlg.ProgressState.None;
    this.lowLevelMsg.Text = "";
    this.lowLevelPrsBar.Value = this.lowLevelPrsBar.Minimum;
    this.midLevelCurrentStep = 0;
    this.midLevelAllSteps = 0;
    this.lowLevelPreMsg.Text = "";
    this.errorPanel.Visibility = Visibility.Hidden;
  }

  private void AccuracySlr_ValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e)
  {
    this.NumAccuracy.Text = this.SliderValue;
  }

  private void RemoveBtn_Click(object sender, RoutedEventArgs e)
  {
    IList selectedItems = this.operationsProgramsGrid.SelectedItems;
    for (int index = selectedItems.Count - 1; index >= 0; --index)
      this.operationsProgramsGrid.Items.Remove(selectedItems[index]);
    this.CreateBtn.IsEnabled = this.ShouldEnableCreateButton();
    bool flag = false;
    foreach (DataGridItem dataGridItem in (IEnumerable) this.operationsProgramsGrid.Items)
    {
      if (dataGridItem.ValidityIconToolTip != null)
      {
        flag = true;
        break;
      }
    }
    if (flag == this.AreInvalidItemsInGrid)
      return;
    this.AreInvalidItemsInGrid = flag;
  }

  private void operationsProgramsGrid_SelectionChanged(object sender, SelectionChangedEventArgs e)
  {
    if (this.operationsProgramsGrid.SelectedItems.Count > 0)
      this.RemoveBtn.IsEnabled = true;
    else
      this.RemoveBtn.IsEnabled = false;
  }

  private void CUIAutomaticInterferenceVolumeCreationDlg_Closing(object sender, CancelEventArgs e)
  {
    if (this.dlgState == CUIAutomaticInterferenceVolumeCreationDlg.DialogState.Progress && !this.CreateBtn.IsEnabled)
      e.Cancel = true;
    if (!this.wasCreationStarted)
      return;
    TxApplication.ActiveUndoManager.ClearAllTransactions();
  }

  private void TxWindow_Closed(object sender, EventArgs e)
  {
    TxApplication.ActiveDocument.Selection.Cleared -= new TxSelection_ClearedEventHandler(this.Selection_Cleared);
    TxApplication.ActiveDocument.Selection.ItemsAdded -= new TxSelection_ItemsAddedEventHandler(this.Selection_ItemsAdded);
    TxApplication.ActiveDocument.Selection.ItemsRemoved -= new TxSelection_ItemsRemovedEventHandler(this.Selection_ItemsRemoved);
    TxApplication.ActiveDocument.Selection.ItemsSet -= new TxSelection_ItemsSetEventHandler(this.Selection_ItemsSet);
    TxApplication.ActiveDocument.SimulationPlayer.SimulationStartedForward -= new TxSimulationPlayer_SimulationStartedForwardEventHandler(this.SimulationPlayer_SimulationStartedForward);
    TxApplication.ActiveDocument.SimulationPlayer.SimulationStartedBackward -= new TxSimulationPlayer_SimulationStartedBackwardEventHandler(this.SimulationPlayer_SimulationStartedBackward);
    TxApplication.ActiveDocument.SimulationPlayer.SimulationStopped -= new TxSimulationPlayer_SimulationStoppedEventHandler(this.SimulationPlayer_SimulationStopped);
    TxApplication.ActiveDocument.SimulationPlayer.SimulationEndedForward -= new TxSimulationPlayer_SimulationEndedForwardEventHandler(this.SimulationPlayer_SimulationEndedForward);
    TxApplication.ActiveDocument.SimulationPlayer.SimulationEndedBackward -= new TxSimulationPlayer_SimulationEndedBackwardEventHandler(this.SimulationPlayer_SimulationEndedBackward);
    this.shouldStop = true;
    if (this.dlgState == CUIAutomaticInterferenceVolumeCreationDlg.DialogState.Progress)
      this.mgr.StopGeneration(false);
    this.mgr.NextHighLevelStep -= this.NextHighLevelStepHandler;
    this.mgr.NextMidLevelStep -= this.NextMidLevelStepHandler;
    this.mgr.LowLevelStepProgress -= this.LowLevelProgresHandler;
    this.mgr.LowLevelStepDuration -= this.LowLevelDurationHandler;
    this.mgr.LogError -= this.LogErrorEventHandler;
    this.mgr.SetAccuracy((TxWindow) this, (int) this.AccuracySlr.Value);
    CApAIVCManager mgr1 = this.mgr;
    bool? isChecked1 = this.keepNISV.IsChecked;
    bool flag1 = true;
    int num1 = isChecked1.GetValueOrDefault() == flag1 & isChecked1.HasValue ? 1 : 0;
    mgr1.SetKeepNISweptVolumes((TxWindow) this, num1 != 0);
    CApAIVCManager mgr2 = this.mgr;
    bool? isChecked2 = this.includeCables.IsChecked;
    bool flag2 = true;
    int num2 = isChecked2.GetValueOrDefault() == flag2 & isChecked2.HasValue ? 1 : 0;
    mgr2.SetIncludeCables((TxWindow) this, num2 != 0);
    this.mgr.SetShowConfirmMsg((TxWindow) this, this.showConfirmStopMsg);
  }

  private void Selection_ItemsSet(object sender, TxSelection_ItemsSetEventArgs args)
  {
    this.ShouldEnableAddBtn(args.ObjectList);
  }

  private void Selection_ItemsRemoved(object sender, TxSelection_ItemsRemovedEventArgs args)
  {
    this.ShouldEnableAddBtn(args.ObjectList);
  }

  private void Selection_ItemsAdded(object sender, TxSelection_ItemsAddedEventArgs args)
  {
    this.ShouldEnableAddBtn(args.ObjectList);
  }

  private void Selection_Cleared(object sender, TxSelection_ClearedEventArgs args)
  {
    if (this.addOperationsAndProgramsBtn != null)
      this.addOperationsAndProgramsBtn.IsEnabled = false;
    this.addOperationsBtn.IsEnabled = false;
    if (this.addProgramsBtn == null)
      return;
    this.addProgramsBtn.IsEnabled = false;
  }

  private bool IsRoboticProgram(ITxObject obj)
  {
    return obj is ITxRoboticProgram && obj is ITxSweptVolumeHoldingObject;
  }

  private bool IsRoboticOperation(ITxObject obj) => obj is ITxRoboticOrderedCompoundOperation;

  private bool IsCompoundOperation(ITxObject obj)
  {
    return obj is ITxCompoundOperation && !(obj is TxObjectFlowOperation) && !(obj is TxRoboticSeamOperation);
  }

  private void ShouldEnableAddBtn(TxObjectList selection)
  {
    int num = 0;
    if (((Collection<ITxObject>) selection).Count >= 1)
    {
      foreach (ITxObject itxObject in (Collection<ITxObject>) selection)
      {
        if (this.IsRoboticOperation(itxObject) || this.IsCompoundOperation(itxObject))
          num |= 1;
        if (this.IsRoboticProgram(itxObject))
          num |= 2;
      }
    }
    this.addOperationsBtn.IsEnabled = false;
    if (this.addProgramsBtn != null)
      this.addProgramsBtn.IsEnabled = false;
    if (this.addOperationsAndProgramsBtn != null)
      this.addOperationsAndProgramsBtn.IsEnabled = false;
    if ((num & 1) != 0)
    {
      this.addOperationsBtn.IsEnabled = true;
      if (this.addProgramsBtn != null)
        this.addProgramsBtn.IsEnabled = true;
      if (this.addOperationsAndProgramsBtn != null)
        this.addOperationsAndProgramsBtn.IsEnabled = true;
    }
    if ((num & 2) == 0)
      return;
    if (this.addProgramsBtn != null)
      this.addProgramsBtn.IsEnabled = true;
    if (this.addOperationsAndProgramsBtn == null)
      return;
    this.addOperationsAndProgramsBtn.IsEnabled = true;
  }

  private AddButton CreateButton(string caption, string icon, ADD_BUTTON_CLICK_TYPE type)
  {
    AddButton button = new AddButton();
    button.Margin = new Thickness(0.0, 0.0, 6.0, 0.0);
    button.Type = type;
    button.Click += new RoutedEventHandler(this.btn_Click);
    button.Content = (object) new System.Windows.Controls.Image()
    {
      Source = (ImageSource) CApAIVCManager.GetImage(icon)
    };
    button.ToolTip = (object) caption;
    button.Style = (Style) ((ResourceDictionary) this.baseRD)[(object) "buttonStyle"];
    return button;
  }

  private void btn_Click(object sender, RoutedEventArgs e)
  {
    this.addOperationsPrograms((sender as AddButton).Type);
  }

  private UIElement[] CreateAddButtonsAndFixColumnName()
  {
    List<UIElement> uiElementList = new List<UIElement>();
    string icon = "pack://application:,,,/DnProcessSimulateCommands;component/AutomaticInterferenceVolumeCreation/Resources/Add_to_table.ico";
    if (!TxTcUtilities.IsTcAnyPlatform)
    {
      this.addProgramsBtn = this.CreateButton(this.mgr.ResourceMgr.GetString("AIVC_ADD_ROB_PR"), "pack://application:,,,/DnProcessSimulateCommands;component/AutomaticInterferenceVolumeCreation/Resources/Add_program_to_table.ico", ADD_BUTTON_CLICK_TYPE.ADD_ROBOTIC_PROGRAMS);
      this.addProgramsBtn.TabIndex = 4;
      uiElementList.Add((UIElement) this.addProgramsBtn);
      icon = "pack://application:,,,/DnProcessSimulateCommands;component/AutomaticInterferenceVolumeCreation/Resources/Add_operation_to_table.ico";
    }
    this.addOperationsBtn = this.CreateButton(this.mgr.ResourceMgr.GetString("AIVC_ADD_ROB_OP"), icon, ADD_BUTTON_CLICK_TYPE.ADD_ROBOTIC_OPERATIONS);
    this.addOperationsBtn.TabIndex = 3;
    uiElementList.Add((UIElement) this.addOperationsBtn);
    if (TxTcUtilities.IsTcAnyPlatform)
    {
      this.operationsProgramsGrid.Columns[0].Header = (object) this.mgr.ResourceMgr.GetString("AIVC_CATEGORY");
    }
    else
    {
      this.addOperationsAndProgramsBtn = this.CreateButton(this.mgr.ResourceMgr.GetString("AIVC_ADD_ROB_PR_OP"), "pack://application:,,,/DnProcessSimulateCommands;component/AutomaticInterferenceVolumeCreation/Resources/Add_to_table.ico", ADD_BUTTON_CLICK_TYPE.ADD_PROGRAMS_AND_OPERATIONS);
      this.addOperationsAndProgramsBtn.TabIndex = 2;
      uiElementList.Add((UIElement) this.addOperationsAndProgramsBtn);
    }
    return uiElementList.ToArray();
  }

  private void addOperationsPrograms(ADD_BUTTON_CLICK_TYPE Type)
  {
    List<DataGridItem> itemsFromSelection = this.mgr.CreateItemsFromSelection(TxApplication.ActiveDocument.Selection, Type);
    bool flag = false;
    foreach (DataGridItem newItem in itemsFromSelection)
    {
      if (!this.operationsProgramsGrid.Items.Contains((object) newItem))
      {
        this.operationsProgramsGrid.Items.Add((object) newItem);
        if (newItem.ValidityIconToolTip != null)
          flag = true;
      }
    }
    if (flag && flag != this.AreInvalidItemsInGrid)
      this.AreInvalidItemsInGrid = flag;
    this.CreateBtn.IsEnabled = this.ShouldEnableCreateButton();
  }

  private bool ShouldEnableCreateButton()
  {
    int num = 0;
    foreach (DataGridItem dataGridItem in (IEnumerable) this.operationsProgramsGrid.Items)
    {
      if (dataGridItem.ValidityIconToolTip == null)
        ++num;
      if (num > 1)
        return true;
    }
    return false;
  }

  private void StopCloseBtn_Click(object sender, RoutedEventArgs e) => ((Window) this).Close();

  private void ProgressExp_Collapsed(object sender, RoutedEventArgs e)
  {
    if (this.SettingsExp != null)
      this.SettingsExp.IsExpanded = true;
    this.RefreshSize();
  }

  private void SettingsExp_Collapsed(object sender, RoutedEventArgs e)
  {
    if (this.ProgressExp != null)
      this.ProgressExp.IsExpanded = true;
    this.RefreshSize();
  }

  private void SettingsExp_Expanded(object sender, RoutedEventArgs e)
  {
    if (this.ProgressExp != null)
      this.ProgressExp.IsExpanded = false;
    this.RefreshSize();
  }

  private void ProgressExp_Expanded(object sender, RoutedEventArgs e)
  {
    if (this.SettingsExp != null)
      this.SettingsExp.IsExpanded = false;
    this.RefreshSize();
  }

  private void logFileHyperlink_RequestNavigate(object sender, RequestNavigateEventArgs e)
  {
    CApAIVCLogManager.OpenFile();
    e.Handled = true;
  }

  private void AdvanceHighLevelStep()
  {
    switch (this.progressState)
    {
      case CUIAutomaticInterferenceVolumeCreationDlg.ProgressState.None:
        this.ChangeIcon(this.init_Image, (object) this.mgr.InProgressIcon);
        this.progressState = CUIAutomaticInterferenceVolumeCreationDlg.ProgressState.Initialising;
        break;
      case CUIAutomaticInterferenceVolumeCreationDlg.ProgressState.Initialising:
        this.ChangeIcon(this.init_Image, (object) this.mgr.DoneIcon);
        this.ChangeIcon(this.gen_SV_Image, (object) this.mgr.InProgressIcon);
        this.progressState = CUIAutomaticInterferenceVolumeCreationDlg.ProgressState.GeneratingSweptVolumeObjects;
        this.lowLevelPreMsg.Text = this.mgr.ResourceMgr.GetString("AIVC_NOW_GENERATING");
        break;
      case CUIAutomaticInterferenceVolumeCreationDlg.ProgressState.GeneratingSweptVolumeObjects:
        this.ChangeIcon(this.gen_SV_Image, (object) this.mgr.DoneIcon);
        this.ChangeIcon(this.detecting_coll_Image, (object) this.mgr.InProgressIcon);
        this.progressState = CUIAutomaticInterferenceVolumeCreationDlg.ProgressState.DetectingCollisions;
        this.lowLevelPreMsg.Text = this.mgr.ResourceMgr.GetString("AIVC_NOW_CHECKING");
        break;
      case CUIAutomaticInterferenceVolumeCreationDlg.ProgressState.DetectingCollisions:
        this.ChangeIcon(this.detecting_coll_Image, (object) this.mgr.DoneIcon);
        this.ChangeIcon(this.gen_IV_Image, (object) this.mgr.InProgressIcon);
        this.progressState = CUIAutomaticInterferenceVolumeCreationDlg.ProgressState.GeneratingInterferenceVolumeObjects;
        this.lowLevelPreMsg.Text = this.mgr.ResourceMgr.GetString("AIVC_NOW_GENERATING");
        break;
      case CUIAutomaticInterferenceVolumeCreationDlg.ProgressState.GeneratingInterferenceVolumeObjects:
        this.ChangeIcon(this.gen_IV_Image, (object) this.mgr.DoneIcon);
        this.ChangeIcon(this.det_Synchr_Locs_Image, (object) this.mgr.InProgressIcon);
        this.progressState = CUIAutomaticInterferenceVolumeCreationDlg.ProgressState.DetectingSynchronizingLocations;
        this.lowLevelPreMsg.Text = this.mgr.ResourceMgr.GetString("AIVC_NOW_SIMULATING");
        break;
      case CUIAutomaticInterferenceVolumeCreationDlg.ProgressState.DetectingSynchronizingLocations:
        this.ChangeIcon(this.det_Synchr_Locs_Image, (object) this.mgr.DoneIcon);
        bool? isChecked = this.keepNISV.IsChecked;
        bool flag = false;
        if (isChecked.GetValueOrDefault() == flag & isChecked.HasValue && this.errorPanel.Visibility == Visibility.Hidden)
        {
          this.ChangeIcon(this.del_SV_Image, (object) this.mgr.InProgressIcon);
          this.progressState = CUIAutomaticInterferenceVolumeCreationDlg.ProgressState.DeletingNonIntersectingSweptVolumeObjects;
          this.lowLevelPreMsg.Text = this.mgr.ResourceMgr.GetString("AIVC_NOW_DELETING");
          break;
        }
        this.progressState = CUIAutomaticInterferenceVolumeCreationDlg.ProgressState.Finished;
        this.lowLevelPreMsg.Text = "";
        break;
      case CUIAutomaticInterferenceVolumeCreationDlg.ProgressState.DeletingNonIntersectingSweptVolumeObjects:
        this.ChangeIcon(this.del_SV_Image, (object) this.mgr.DoneIcon);
        this.progressState = CUIAutomaticInterferenceVolumeCreationDlg.ProgressState.Finished;
        this.lowLevelPreMsg.Text = "";
        break;
    }
    this.lowLevelMsg.Text = "";
    this.lowLevelPrsBar.Value = this.lowLevelPrsBar.Minimum;
    this.midLevelCurrentStep = 0;
    this.midLevelAllSteps = 0;
    this.lowLevelSimulationOverallDuration = 0.0;
    this.lowLevelSimulationCurrentStep = 0.0;
    System.Windows.Forms.Application.DoEvents();
  }

  private void ChangeIcon(PictureBox img, object icon) => img.Image = (System.Drawing.Image) icon;

  private void TxWindow_Loaded(object sender, RoutedEventArgs e)
  {
    this.keepNISV.IsChecked = new bool?(this.mgr.GetKeepNISweptVolumes((TxWindow) this));
    this.includeCables.IsChecked = new bool?(this.mgr.GetIncludeCables((TxWindow) this));
    this.AccuracySlr.Value = (double) this.mgr.GetAccuracy((TxWindow) this);
    this.showConfirmStopMsg = this.mgr.GetShowConfirmMsg((TxWindow) this);
    this.settingsGrid.DataContext = (object) this;
  }

  private void TxWindow_SourceInitialized(object sender, EventArgs e) => this.SetMinWidth();

  private void SetMinWidth()
  {
    double actualWidth = ((FrameworkElement) this).ActualWidth;
    SizeToContent sizeToContent = ((Window) this).SizeToContent;
    // ISSUE: explicit non-virtual call
    __nonvirtual (((FrameworkElement) this).Width) = 355.0;
    ((Window) this).SizeToContent = SizeToContent.Width;
    ((FrameworkElement) this).MinWidth = ((FrameworkElement) this).ActualWidth;
    ((Window) this).SizeToContent = sizeToContent;
    // ISSUE: explicit non-virtual call
    __nonvirtual (((FrameworkElement) this).Width) = ((FrameworkElement) this).MinWidth < actualWidth ? actualWidth : ((FrameworkElement) this).MinWidth;
  }

  public event PropertyChangedEventHandler PropertyChanged;

  private void FirePropertyChanged(string prop)
  {
    if (this.PropertyChanged == null)
      return;
    this.PropertyChanged((object) this, new PropertyChangedEventArgs(prop));
  }

  private void SimulationPlayer_SimulationEndedBackward(
    object sender,
    TxSimulationPlayer_SimulationEndedBackwardEventArgs args)
  {
    this.CreateBtn.IsEnabled = this.ShouldEnableCreateButton();
  }

  private void SimulationPlayer_SimulationEndedForward(
    object sender,
    TxSimulationPlayer_SimulationEndedForwardEventArgs args)
  {
    this.CreateBtn.IsEnabled = this.ShouldEnableCreateButton();
  }

  private void SimulationPlayer_SimulationStopped(
    object sender,
    TxSimulationPlayer_SimulationStoppedEventArgs args)
  {
    this.CreateBtn.IsEnabled = this.ShouldEnableCreateButton();
  }

  private void SimulationPlayer_SimulationStartedBackward(
    object sender,
    TxSimulationPlayer_SimulationStartedBackwardEventArgs args)
  {
    this.CreateBtn.IsEnabled = false;
  }

  private void SimulationPlayer_SimulationStartedForward(
    object sender,
    TxSimulationPlayer_SimulationStartedForwardEventArgs args)
  {
    this.CreateBtn.IsEnabled = false;
  }

  private void rootWindow_SizeChanged(object sender, SizeChangedEventArgs e) => this.RefreshSize();

  [DebuggerNonUserCode]
  [GeneratedCode("PresentationBuildTasks", "4.0.0.0")]
  public void InitializeComponent()
  {
    if (this._contentLoaded)
      return;
    this._contentLoaded = true;
    System.Windows.Application.LoadComponent((object) this, new Uri("/DnProcessSimulateCommands;component/automaticinterferencevolumecreation/cuiautomaticinterferencevolumecreationdlg.xaml", UriKind.Relative));
  }

  [DebuggerNonUserCode]
  [GeneratedCode("PresentationBuildTasks", "4.0.0.0")]
  [EditorBrowsable(EditorBrowsableState.Never)]
  void IComponentConnector.Connect(int connectionId, object target)
  {
    switch (connectionId)
    {
      case 1:
        this.rootWindow = (CUIAutomaticInterferenceVolumeCreationDlg) target;
        ((Window) this.rootWindow).Closed += new EventHandler(this.TxWindow_Closed);
        ((Window) this.rootWindow).Closing += new CancelEventHandler(this.CUIAutomaticInterferenceVolumeCreationDlg_Closing);
        ((FrameworkElement) this.rootWindow).Loaded += new RoutedEventHandler(this.TxWindow_Loaded);
        ((FrameworkElement) this.rootWindow).SizeChanged += new SizeChangedEventHandler(this.rootWindow_SizeChanged);
        ((Window) this.rootWindow).SourceInitialized += new EventHandler(this.TxWindow_SourceInitialized);
        break;
      case 2:
        this.CreateBtn = (System.Windows.Controls.Button) target;
        this.CreateBtn.Click += new RoutedEventHandler(this.CreateBtn_Click);
        break;
      case 3:
        this.StopCloseBtn = (System.Windows.Controls.Button) target;
        this.StopCloseBtn.Click += new RoutedEventHandler(this.StopCloseBtn_Click);
        break;
      case 4:
        this.ProgressExp = (Expander) target;
        this.ProgressExp.Expanded += new RoutedEventHandler(this.ProgressExp_Expanded);
        this.ProgressExp.Collapsed += new RoutedEventHandler(this.ProgressExp_Collapsed);
        break;
      case 5:
        this.init_Image = (PictureBox) target;
        break;
      case 6:
        this.gen_SV_Image = (PictureBox) target;
        break;
      case 7:
        this.detecting_coll_Image = (PictureBox) target;
        break;
      case 8:
        this.gen_IV_Image = (PictureBox) target;
        break;
      case 9:
        this.det_Synchr_Locs_Image = (PictureBox) target;
        break;
      case 10:
        this.del_SV_Image = (PictureBox) target;
        break;
      case 11:
        this.deleting_SV_objects_TextBlock = (TextBlock) target;
        break;
      case 12:
        this.lowLevelPreMsg = (TextBlock) target;
        break;
      case 13:
        this.lowLevelMsg = (TextBlock) target;
        break;
      case 14:
        this.lowLevelPrsBar = (System.Windows.Controls.ProgressBar) target;
        break;
      case 15:
        this.errorPanel = (StackPanel) target;
        break;
      case 16 /*0x10*/:
        this._imError = (System.Windows.Controls.Image) target;
        break;
      case 17:
        this.errors_reported = (TextBlock) target;
        break;
      case 18:
        this.logFileHyperlink = (Hyperlink) target;
        this.logFileHyperlink.RequestNavigate += new RequestNavigateEventHandler(this.logFileHyperlink_RequestNavigate);
        break;
      case 19:
        this.SettingsExp = (Expander) target;
        this.SettingsExp.Expanded += new RoutedEventHandler(this.SettingsExp_Expanded);
        this.SettingsExp.Collapsed += new RoutedEventHandler(this.SettingsExp_Collapsed);
        break;
      case 20:
        this.settingsGrid = (Grid) target;
        break;
      case 21:
        this.ButtonsToolbar = (StackPanel) target;
        break;
      case 22:
        this.RemoveBtn = (System.Windows.Controls.Button) target;
        this.RemoveBtn.Click += new RoutedEventHandler(this.RemoveBtn_Click);
        break;
      case 23:
        this.operationsProgramsGrid = (System.Windows.Controls.DataGrid) target;
        this.operationsProgramsGrid.SelectionChanged += new SelectionChangedEventHandler(this.operationsProgramsGrid_SelectionChanged);
        break;
      case 24:
        this.NumAccuracy = (TextBlock) target;
        break;
      case 25:
        this.AccuracySlr = (Slider) target;
        this.AccuracySlr.ValueChanged += new RoutedPropertyChangedEventHandler<double>(this.AccuracySlr_ValueChanged);
        break;
      case 26:
        this.keepNISV = (System.Windows.Controls.CheckBox) target;
        break;
      case 27:
        this.includeCables = (System.Windows.Controls.CheckBox) target;
        break;
      default:
        this._contentLoaded = true;
        break;
    }
  }

  private enum DialogState
  {
    Settings,
    Progress,
  }

  private enum ProgressState
  {
    None,
    Initialising,
    GeneratingSweptVolumeObjects,
    DetectingCollisions,
    GeneratingInterferenceVolumeObjects,
    DetectingSynchronizingLocations,
    DeletingNonIntersectingSweptVolumeObjects,
    Finished,
  }
}
